<template>
  <div class="register-container">
    <div class="register-form">
      <h2>用户注册</h2>
      <div class="form-group">
        <label>用户名</label>
        <input v-model="account" type="text" placeholder="请输入用户名" />
      </div>
      <div class="form-group">
        <label>手机号</label>
        <input v-model="phone" type="tel" placeholder="请输入手机号" />
      </div>
      <div class="form-group">
        <label>密码</label>
        <input v-model="password" type="password" placeholder="请输入密码" />
      </div>
      <div class="form-group">
        <label>确认密码</label>
        <input v-model="confirmPassword" type="password" placeholder="请再次输入密码" />
      </div>
      <button @click="register" :disabled="loading">
        {{ loading ? '注册中...' : '注册' }}
      </button>
      <p class="link-text">已有账号？<a href="/">立即登录</a></p>
      <p v-if="error" class="error-message">{{ error }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  data() {
    return {
      account: '',
      phone: '',
      password: '',
      confirmPassword: '',
      loading: false,
      error: ''
    }
  },
  methods: {
    async register() {
      // 简单的客户端验证
      if (!this.account.trim()) {
        ElMessage.warning('请输入用户名')
        return
      }
      
      if (!this.phone.trim()) {
        ElMessage.warning('请输入手机号')
        return
      }
      
      // 简单的手机号格式验证
      const phoneRegex = /^1[3-9]\d{9}$/
      if (!phoneRegex.test(this.phone)) {
        ElMessage.warning('请输入正确的手机号格式')
        return
      }
      
      if (!this.password) {
        ElMessage.warning('请输入密码')
        return
      }
      
      if (this.password.length < 6) {
        ElMessage.warning('密码长度不能少于6位')
        return
      }
      
      if (this.password !== this.confirmPassword) {
        ElMessage.warning('两次输入的密码不一致')
        return
      }
      
      this.loading = true
      this.error = ''
      
      try {
        const response = await axios.post('/auth/register', {
          account: this.account,
          phone: this.phone,
          password: this.password,
          confirmPassword: this.confirmPassword
        })
        
        if (response.data.code === 200) {
          // 注册成功后跳转到登录页面
          ElMessage.success('注册成功，请登录')
          this.$router.push('/')
        } else {
          ElMessage.error(response.data.message)
        }
      } catch (error) {
        ElMessage.error('注册失败，请稍后重试')
        console.error('注册请求失败:', error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('/assets/background.jpg') no-repeat center center fixed;
  background-size: cover;
  position: relative;
}

/* 背景遮罩 */
.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1;
}

.register-form {
  background: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  width: 400px;
  position: relative;
  z-index: 2;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

input:focus {
  outline: none;
  border-color: #4CAF50;
}

button {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover:not(:disabled) {
  background-color: #45a049;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.link-text {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.link-text a {
  color: #4CAF50;
  text-decoration: none;
}

.link-text a:hover {
  text-decoration: underline;
}

.error-message {
  color: #f44336;
  text-align: center;
  margin-top: 15px;
}
</style>